package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketGoodsAttribute;
import com.cskaoyan.bean.MarketGoodsProduct;
import com.cskaoyan.bean.MarketGoodsSpecification;
import com.cskaoyan.bean.bo.AdminGoodsListBO;
import com.cskaoyan.bean.bo.CreateGoodsBO;
import com.cskaoyan.bean.bo.GoodsDetailBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminGoodsVO;
import com.cskaoyan.bean.vo.BrandOfCatAndBrandVO;
import com.cskaoyan.bean.vo.CatAndBrandVO;
import com.cskaoyan.bean.vo.CatOfCatAndBrandVO;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : gao
 * @since: 2022/10/11  22:59
 */
@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketGoodsAttributeMapper attributeMapper;


    @Autowired
    MarketCategoryMapper categoryMapper;

    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public BaseRespVo queryGoodsList(AdminGoodsListBO adminGoodsListBO) {


        Integer page = adminGoodsListBO.getPage();
        Integer limit = adminGoodsListBO.getLimit();
        List<AdminGoodsVO> goodsVOList=null;
        if(page!=null){
            //Date[] sort = adminGoodsListBO.getSort();
//        String sort = adminGoodsListBO.getSort();
//        String order = adminGoodsListBO.getOrder();

            PageHelper.startPage(page, limit);
             goodsVOList = goodsMapper.selectGoodsList(adminGoodsListBO);
        }else{
            goodsVOList = goodsMapper.selectGoodsList(adminGoodsListBO);
        }
        PageInfo<AdminGoodsVO> pageInfo = new PageInfo<>(goodsVOList);


//        PageHelper.startPage(page, limit);
//        String[] listGallery = goodsMapper.selectGallery();
//        AdminGoodsVO adminGoodsVO = new AdminGoodsVO();
//        adminGoodsVO.setGallery(listGallery);

        CommonData<AdminGoodsVO> commonData = new CommonData<>();
        commonData.setTotal((int) pageInfo.getTotal());
        commonData.setPages(pageInfo.getPages());
        commonData.setLimit(limit);
        commonData.setPage(page);
        commonData.setList(goodsVOList);

        BaseRespVo<CommonData> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setData(commonData);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;


    }

    @Override
    public CatAndBrandVO queryCatAndBrandVO() {
        CatAndBrandVO catAndBrandVO = new CatAndBrandVO();

        // CatOfCatAndBrandVO catOfCatAndBrandVO = new CatOfCatAndBrandVO();
        List<CatOfCatAndBrandVO> catOfCatAndBrandVOList = categoryMapper.selectIdAndNameCat();
        List<BrandOfCatAndBrandVO> children = new ArrayList<>();
        for (CatOfCatAndBrandVO catList : catOfCatAndBrandVOList) {
            Integer id = catList.getValue();
            children = categoryMapper.selectIdAndNameCatByPid(id);
            catList.setChildren(children);
        }


        List<BrandOfCatAndBrandVO> brandList = brandMapper.selectIdAndName();

        catAndBrandVO.setCategoryList(catOfCatAndBrandVOList);
        catAndBrandVO.setBrandList(brandList);
        return catAndBrandVO;
    }

    /**
     * 商品上架
     *
     * @return {@link null}
     * @author gao
     * @since 2022/10/12 19:09
     */
    @Override
    public void createGoods(CreateGoodsBO createGoodsBO) {
        MarketGoods goods = createGoodsBO.getGoods();
        Date date = new Date();
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        //添加goods信息
        goodsMapper.insertSelective(goods);
        Integer goodsId = goods.getId();

        //获取规格信息的集合，进行添加
        List<MarketGoodsSpecification> specifications = createGoodsBO.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(goodsId);
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            specificationMapper.insertSelective(specification);
        }

        List<MarketGoodsProduct> products = createGoodsBO.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setGoodsId(goodsId);
            product.setAddTime(date);
            product.setUpdateTime(date);
            productMapper.insertSelective(product);
        }


        List<MarketGoodsAttribute> attributes = createGoodsBO.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsId);
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            attributeMapper.insertSelective(attribute);
        }


    }

    /**
     * 编辑商品详细信息回显
     *
     * @return {@link null}
     * @author gao
     * @since 2022/10/12 21:50
     */
    @Override
    public GoodsDetailBO detailGoods(Integer id) {
        GoodsDetailBO goodsDetailBO = new GoodsDetailBO();

        Integer[] categoryIds = goodsMapper.selectCategoryIdsByIds(id);
        MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(id);
        List<MarketGoodsAttribute> attributeList = attributeMapper.selectByGoodsId(id);
        List<MarketGoodsSpecification> specificationList = specificationMapper.selectByGoodsId(id);
        List<MarketGoodsProduct> productList = productMapper.selectByGoodsId(id);

        goodsDetailBO.setCategoryIds(categoryIds);
        goodsDetailBO.setGoods(marketGoods);
        goodsDetailBO.setAttributes(attributeList);
        goodsDetailBO.setSpecifications(specificationList);
        goodsDetailBO.setProducts(productList);

        return goodsDetailBO;
    }

    /**
         *  删除商品
         * @return {@link null}
         * @author gao
         * @since 2022/10/13 0:27
         */
    @Override
    public void deleteGoods(Integer id) {
        goodsMapper.deleteByPrimaryKey(id);
        attributeMapper.deleteByPrimaryKey(id);
        productMapper.deleteByPrimaryKey(id);
        specificationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateGoods(CreateGoodsBO createGoodsBO) {
        MarketGoods goods = createGoodsBO.getGoods();
        goodsMapper.updateByPrimaryKey(goods);

        List<MarketGoodsSpecification> specifications = createGoodsBO.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specificationMapper.updateByPrimaryKeySelective(specification);
        }

        List<MarketGoodsProduct> products = createGoodsBO.getProducts();
        for (MarketGoodsProduct product : products) {
            productMapper.updateByPrimaryKeySelective(product);
        }

        //更新参数信息
        Integer goodsId = goods.getId();
        attributeMapper.deleteByPrimaryKey(goodsId);
        List<MarketGoodsAttribute> attributes = createGoodsBO.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsId);
            Date date = new Date();
            attribute.setUpdateTime(date);
            attributeMapper.insertSelective(attribute);
        }
    }
}
